package com.suishoupai.xmb.utils;

import org.springframework.util.StringUtils;

/**
 * 价格工具
 *
 * @author caojian
 * @version 1.0.0
 */
public final class PriceUtil {

    /**
     * 讲元转为分
     *
     * @return
     */
    public static int yuan2fen(String price) {
        if (StringUtils.isEmpty(price)) {
            return 0;
        }
        int index = price.indexOf(".");
        if (index == -1) {
            price += "00";
        } else {
            if (price.length() - (index + 1) > 2) {
                price = price.substring(0, index + 1 + 2);
            } else if (price.length() - (index + 1) == 2) {
                // price=price
            } else if (price.length() - (index + 1) < 2) {
                price += (price.length() - (index + 1)) == 1 ? "0" : "00";
            }
        }
        price = price.replace(".", "");
        return Integer.parseInt(price);
    }
}
